function processFile(blob, fileName) { var out = document.getElementById('box2'); function flashCopiedUi() { var status = document.getElementById('clipboard-status'); var check = document.getElementById('clipboard-check'); if (typeof showClipboardNotification === 'function') { showClipboardNotification(); } else { if (status) status.style.display = 'block'; if (check) check.style.display = 'inline'; } var btns = document.querySelectorAll('.copy-btn'); for (var i = 0; i < btns.length; i++) { btns[i].innerHTML = ' Copied!'; } if (btns.length) { setTimeout(function () { for (var i = 0; i < btns.length; i++) { btns[i].innerHTML = ''; } }, 2000); } } function autoCopyResultText(value) { if (!value) return; function fallbackCopy() { if (!out) return; var prevStart = out.selectionStart; var prevEnd = out.selectionEnd; var previousFocus = document.activeElement; try { out.focus(); out.select(); if (document.execCommand('copy')) { flashCopiedUi(); } } catch (_) { } try { if (typeof prevStart === 'number' && typeof prevEnd === 'number' && out.setSelectionRange) { out.setSelectionRange(prevStart, prevEnd); } } catch (_) { } try { if (previousFocus && previousFocus !== out && typeof previousFocus.focus === 'function') { previousFocus.focus(); } } catch (_) { } } if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(value).then(function () { flashCopiedUi(); }).catch(function () { fallbackCopy(); }); } else { fallbackCopy(); } } if (out) out.value = 'Loading OCR engine (first run downloads ~10 MB)...'; loadScriptPromise('https://cdn.jsdelivr.net/npm/tesseract.js@5.1.1/dist/tesseract.min.js').then(function() { if (!window.Tesseract) { alert('OCR library failed to load.'); return; } var imgUrl = URL.createObjectURL(blob); Tesseract.recognize(imgUrl, 'eng', { logger: function(m) { if (out && m && m.status) { var pct = m.progress != null ? ' ' + Math.round(m.progress * 100) + '%' : ''; out.value = m.status + pct; } } }).then(function(result) { var text = (result && result.data && result.data.text) || ''; if (out) out.value = text.trim() ? text : '(No text was detected in this image.)'; if (text && text.trim()) autoCopyResultText(text); var base = (fileName || 'image').replace(/\.[^.]+$/, ''); var txtBlob = new Blob([text], { type: 'text/plain;charset=utf-8' }); add_file_output(URL.createObjectURL(txtBlob), base + '.txt'); URL.revokeObjectURL(imgUrl); $('#waiting').hide(); $('#social').fadeIn(2000); }).catch(function(err) { if (out) out.value = ''; alert('OCR failed: ' + (err && err.message || err)); URL.revokeObjectURL(imgUrl); }); }).catch(function() { alert('Could not load the OCR library.'); }); } var _loadedScripts = {}; function loadScriptPromise(url) { if (_loadedScripts[url]) return _loadedScripts[url]; _loadedScripts[url] = new Promise(function (resolve, reject) { var s = document.createElement('script'); s.src = url; s.onload = resolve; s.onerror = reject; document.head.appendChild(s); }); return _loadedScripts[url]; } function replaceAll(find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } function beautify(str) { var result = ''; var length = str.length; var i = 0; var braceCountLeft = 0; var braceCountRight = 0; var withinQuotes = false; while (i < length) { var c = str[i]; if (c == '"' && (i == 0 || c[i - 1] != '\\')) { // non-escaped quotes withinQuotes = !withinQuotes; } if (!withinQuotes && (c == '}' || c == '{' || c == ',')) { console.log('Start####' + result); // look back and remove carriage returns and whitespace that are already there var resultIndex = result.length - 1; while (resultIndex >= 0 && (result[resultIndex] == ' ' || result[resultIndex] == '\r' || result[resultIndex] == '\n' || result[resultIndex] == '\t')) { resultIndex = resultIndex - 1; result = result.substr(0, resultIndex + 1); console.log('char ' + result[resultIndex] + '-----' + result + 'zzz ' + result.length + ' ' + resultIndex); } if (c == '{') { braceCountLeft++; result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } else if (c == '}') { braceCountRight++; // precede with carriage return result += '\r' + GetTabs(braceCountLeft - braceCountRight) + c; } else if (c == ',') { result += c + '\r' + GetTabs(braceCountLeft - braceCountRight); } var nextChar = ''; // advance through whitespace and remove carriage returns that are already there while (i < length && (str[i + 1] == ' ' || str[i + 1] == '\r' || str[i + 1] == '\n' || str[i + 1] == '\t')) { i++; } } else { result += str[i]; } i++; } return result; } function GetTabs(count) { var result = ''; for (var i = 0; i < count; i++) { result += ' '; } return result; }